package cn.hjm.hrm.web.controller;

import cn.hjm.hrm.domain.Order;
import cn.hjm.hrm.domain.Productstore;
import cn.hjm.hrm.dto.ProductDto;
import cn.hjm.hrm.feign.client.OrderFeginClient;
import cn.hjm.hrm.feign.client.StoreFeginClient;
import cn.hjm.hrm.query.ProductQuery;
import cn.hjm.hrm.service.IProductService;
import cn.hjm.hrm.domain.Product;
import cn.hjm.util.AjaxResult;
import cn.hjm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    public IProductService productService;
    @Autowired
    private StoreFeginClient storeFeginClient;
    @Autowired
    private OrderFeginClient orderFeginClient;

    @PostMapping("/create")
    public AjaxResult create(@RequestBody ProductDto productDto){
        try {
            Long id = productDto.getId();
            Productstore store = storeFeginClient.getById(id);
            System.out.println(store);
            if (store.getNumber()>productDto.getNumber()){
                Order createorder = orderFeginClient.createorder(productDto);
                storeFeginClient.getNumber(productDto);
                //返回订单号
                return AjaxResult.me().setResultObj(createorder.getOrderNo());
            }else {
                return AjaxResult.me().setSuccess(false).setResultObj("我们正在积极采购。。");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务瓦了"+e.getMessage());
        }
    }


    /**
    * 保存和修改公用的
    * @param product  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody Product product){
        try {
            if(product.getId()!=null){
                productService.updateById(product);
            }else{
                productService.insert(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Product get(@PathVariable("id")Long id)
    {
        return productService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Product> list(){

        return productService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<Product> json(@RequestBody ProductQuery query)
    {
        Page<Product> page = new Page<Product>(query.getPage(),query.getRows());
        page = productService.selectPage(page);
        return new PageList<Product>(page.getTotal(),page.getRecords());
    }
}
